package com.hdw.cms.service.impl;

import java.io.File;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hdw.cms.service.TopicService;
import com.hdw.cms.utils.UeditorUtil;
import com.hdw.basic.model.Pager;
import com.hdw.basic.model.SystemContext;
import com.hdw.cms.dao.AttachmentDao;
import com.hdw.cms.dao.ChannelDao;
import com.hdw.cms.dao.TopicDao;
import com.hdw.cms.dao.UserDao;
import com.hdw.cms.model.Attachment;
import com.hdw.cms.model.Channel;
import com.hdw.cms.model.ChannelType;
import com.hdw.cms.model.CmsException;
import com.hdw.cms.model.Topic;
import com.hdw.cms.model.User;

@Service("topicService")
public class TopicServiceImpl implements TopicService {

	@Resource(name = "topicDao")
	private TopicDao topicDao;

	// @Resource(name="attachmentDao")
	// private AttachmentDao attachmentDao;

	@Resource(name = "channelDao")
	private ChannelDao channelDao;
	
	@Resource(name = "attachmentDao")
	private AttachmentDao attachmentDao;

	@Resource(name = "userDao")
	private UserDao userDao;

	// 添加文章附件
	// private void addTopicAtts(Topic topic,Integer[] aids) {
	// if(aids!=null) {
	// for(Integer aid:aids) {
	// Attachment a = attachmentDao.load(aid);
	// if(a==null) continue;
	// }
	// }
	// }

	// 添加文章
	@Override
	public void add(Topic topic, int cid, int uid, Integer[] aids) {
		Channel c = channelDao.load(cid);// 获取所要添加的栏目ID，装载栏目对象
		User u = userDao.load(uid);// 根据uid装载用户
		if (c == null)
			throw new CmsException("要添加的文章必须有栏目");
		String topicAttName = UeditorUtil.getUeFileName(topic.getContent());
		if (topicAttName != null) {
			topic.setTopicAttName(topicAttName);
		}
		topic.setAuthor(u.getNickname());// 设置发布文章的人
		topic.setCname(c.getName());// a设置文章所在的栏目
		topic.setCreateDate(new Date());// 设置文章创建时间
		topic.setChannel(c);// 给文章设置栏目
		topic.setUser(null);//
		topicDao.add(topic);// 添加文章
		// addTopicAtts(topic, aids);//维护文章和附件的关系
	}

	@Override // 添加没有附件的文章
	public void add(Topic topic, int cid, int uid) {
		add(topic, cid, uid, null);
	}

	// 删除文章，同时删除附件
	@Override
	public void delete(int id) {
		// 先根据文章获取所有的附件
		// List<Attachment> atts = attachmentDao.listByTopic(id);
		Topic topic = topicDao.load(id);
		// 删除附件和ID关系
		// attachmentDao.deleteByTopic(id);
		// 删除文章
		topicDao.delete(id);
		String[] filename = UeditorUtil.dealUeFileName(topic.getTopicAttName());
		if (filename != null) {
			String realpath = SystemContext.getRealPath() + "/upload/";
			for (int i = 0; i < filename.length; i++) {
				new File(realpath + filename[i]).delete();
			}
		}
		// 删除硬盘上面的文件
		// for(Attachment a:atts) {
		// AttachmentServiceImpl.deleteAttachFiles(a);
		// }
	}

	@Override // 更新文章同时更新附件
	public void update(Topic topic, int cid, Integer[] aids) {
		Channel c = channelDao.load(cid);
		if (c == null)
			throw new CmsException("要更新的文章必须有用户和栏目");
		String topicAttName = UeditorUtil.getUeFileName(topic.getContent());
		if (topicAttName != null) {
			topic.setTopicAttName(topicAttName);
		}
		topic.setCname(c.getName());
		topic.setChannel(c);
		topicDao.update(topic);
		// addTopicAtts(topic, aids);
	}

	// 只更新文章不更新附件
	@Override
	public void update(Topic topic, int cid) {
		update(topic, cid, null);
	}

	// 根据文章ID导入文章
	@Override
	public Topic load(int id) {
		return topicDao.load(id);
	}

	// 根据cid,title,status查询文章并分页
	@Override
	public Pager<Topic> find(Integer cid, String title, Integer status) {
		return topicDao.find(cid, title, status);
	}

	// 根据uid,cid,title,status查询文章并分页
	@Override
	public Pager<Topic> find(Integer uid, Integer cid, String title, Integer status) {
		return topicDao.find(uid, cid, title, status);
	}

	// 根据Keyword查询文章并分页
	@Override
	public Pager<Topic> searchTopicByKeyword(String keyword) {
		return topicDao.searchTopicByKeyword(keyword);
	}

	// 根据内容查询文章并分页
	@Override
	public Pager<Topic> searchTopic(String con) {
		return topicDao.searchTopic(con);
	}

	// 查询推荐文章并分页
	@Override
	public Pager<Topic> findRecommendTopic(Integer ci) {
		return topicDao.findRecommendTopic(ci);
	}

	// 更新文章状态
	@Override
	public void updateStatus(int tid) {
		Topic t = topicDao.load(tid);
		if (t.getStatus() == 0)
			t.setStatus(1);
		else
			t.setStatus(0);
		topicDao.update(t);
	}

	@Override
	public List<Topic> listTopicByChannelAndNumber(int cid, int num) {
		return topicDao.listTopicByChannelAndNumber(cid, num);
	}

	@Override
	public List<Topic> listTopicByChannel(int cid) {
		return topicDao.listTopicsByChannel(cid);
	}

	@Override
	public boolean isUpdateIndex(int cid) {
		return topicDao.isUpdateIndex(cid);
	}

	@Override
	public Topic loadLastedTopicByColumn(int cid) {
		return topicDao.loadLastedTopicByColumn(cid);
	}

	@Override
	public List<Topic> listTopicByChannelType(ChannelType ct) {
		return topicDao.listTopicsByChannelType(ct);
	}

	@Override
	public List<Topic> listTopicsByChannelNum(int cid) {
		return topicDao.listTopicsByChannelNum(cid);
	}

	@Override
	public List<String> ListUnUseFiles() {
		List<Topic> topic = topicDao.ListAllTopic();
		List<Attachment> att=attachmentDao.listProAndVideo();
		LinkedList<String> fileName1 = new LinkedList<String>();
		LinkedList<String> fileName2 = new LinkedList<String>();
		String[] allFilesNames = (new File(SystemContext.getRealPath() + "upload")).list();
		for (Topic t : topic) {
			for (String fileName : UeditorUtil.dealUeFileName(t.getTopicAttName())) {
				fileName1.add(fileName);
			}
		}
		for(Attachment a:att)
		{
			for (String fileName : UeditorUtil.dealUeFileName(a.getUnUseFile())) {
				fileName1.add(fileName);
			}
		}
		if (allFilesNames != null) {
			for (String fileName : allFilesNames) {
				if (!fileName1.contains(fileName)) {
					fileName2.add(fileName);
				}
			}
		}
		return fileName2;
	}

	@Override
	public void delUnUseFiles(String fileName) {
		String path = SystemContext.getRealPath();
		new File(path + "/upload/" + fileName).delete();
	}

	public List<Topic> ListReTopic() {
		return topicDao.ListReTopic();
	}
	// public static void main(String[] args) {
	// File file=new File(SystemContext.getRealPath()+"/upload");
	// String [] files=file.list();
	// for(String f:files){
	// System.out.println(f);
	// }
	// System.out.println(SystemContext.getRealPath());
	// }

	@Override
	public void updateContent(Topic t) {
		topicDao.updateContent(t);
	}

	@Override
	public List<Topic> ListAllTopic() {
		return topicDao.ListAllTopic();
	}

/*	@Override
	public void changeAttPath() {
		topicDao.changeAttPath();
		
	}*/

	
}
